package swag.t;

import java.util.ArrayList;
import java.util.Random;

import android.content.Context;
import android.util.Log;
import android.view.View;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.View.OnTouchListener;
import android.view.View;
import android.widget.Toast;
public class DrawView extends View


{
	Ball ball1;
	public int Difficulty = 1;
	private Paint paint;
	private EvilDoer evildoer1;
	private Pixel pixel1;
	public int level = 1;
	Random generator = new Random();  
	ArrayList<Pixel> PixelList = new ArrayList <Pixel>();
	ArrayList<EvilDoer> EvilDoerList = new ArrayList <EvilDoer>();
	int score=0;
	boolean genEvilDoer;

	Context context;


	//Is a factory required to use bitmaps
	BitmapFactory.Options ops = new BitmapFactory.Options();
	//contains all of the backgrounds
	Bitmap[] backgrounds=  new Bitmap[10];



	public DrawView(Context context) {
		super(context);
		setFocusable(true);
		this.context=context;
		ball1 = new Ball(300, 300, 5, 5, Color.BLUE,20, context, 3, R.drawable.blobicon);
		evildoer1 = new EvilDoer(60,60, 5,5,Color.RED,20,context, R.drawable.blobiconred);
		pixel1 = new Pixel(200,200,10,Color.WHITE,context, R.drawable.pixel);
		generateEvilDoer(1);  	 
		generatePixels(20);
		setFocusableInTouchMode(true);
		//sets up the factory
		ops.inJustDecodeBounds=true;
		// creates the background from file
		Bitmap temp=BitmapFactory.decodeResource(context.getResources(), R.drawable.background2);
		//scales background and sets in in array
		backgrounds[0]=Bitmap.createScaledBitmap(temp,600,700,false);
	}

	public void generatePixels(int numPixels)
	{
		for(int i=0; i<numPixels;i++)
		{
			int y = generator.nextInt(650);
			int x = generator.nextInt(420);
			PixelList.add(new Pixel(x,y, 20, Color.WHITE, context, R.drawable.pixel));
			System.out.println("PixelGenerate "+x +" "+y);
		}
	}

	public void setDifficulty(int difficultynum)
	{
		Difficulty = difficultynum;
	}

	public void setLives(int lifenumber)
	{
		ball1.setlives(lifenumber);
	}


	public void generateEvilDoer(int numEvilDoers)
	{
		for(int i = 0; i<numEvilDoers; i++)
		{
			int y = generator.nextInt(680);
			int x = generator.nextInt(450);
			EvilDoerList.add(new EvilDoer(x,y,5,5,Color.RED,20,getContext(), R.drawable.blobiconred));
		}
	}

	@Override
	public void onDraw(Canvas c) {
		paint = new Paint();
		//draws background
		c.drawBitmap(backgrounds[0], 0, 0, paint);




		paint.setColor(Color.BLUE);
		paint.setAntiAlias(true);
		ArrayList<Pixel> remove1 = ball1.move(0, 0, PixelList); 

		//		c.drawCircle(ball1.getx(),ball1.gety(), ball1.getradius(), paint);
		c.drawBitmap(ball1.getImage(), ball1.getx()-ball1.getradius(), ball1.gety()-ball1.getradius(), paint);
		for(int i = 0; i < remove1.size(); i++)
		{
			PixelList.remove(remove1.get(i));
			score++;

		}

		for (int i=0; i<EvilDoerList.size(); i++)
		{
			EvilDoer evildoer1=EvilDoerList.get(i);
			paint.setColor(evildoer1.getcolor());
			evildoer1.move(1,1,ball1);
			
			c.drawBitmap(evildoer1.getImage(), evildoer1.getx()-evildoer1.getradius(), evildoer1.gety()-evildoer1.getradius(), paint);
			//c.drawCircle(evildoer1.getx(), evildoer1.gety(), evildoer1.getradius(), paint);
		}
		for (int i = 0; i<PixelList.size();i++)
		{
			Pixel pixel1=PixelList.get(i);
			paint.setColor(pixel1.getcolor());
			//c.drawRect(pixel1.getx(), pixel1.gety(), pixel1.getx()+pixel1.getsidelength(), pixel1.gety()+pixel1.getsidelength(), paint);
			c.drawBitmap(pixel1.getImage(), pixel1.getx()-pixel1.getsidelength(),pixel1.gety()-pixel1.getsidelength(), paint);
			
			//  c.drawCircle(ball1.getX(), ball1.getY(), ball1.getRadius(), paint);//Ball class manages the circle (its coordinates, color, velocity, etc.)
		}
		//	c.drawBitmap(evildoer1.getImage(),evildoer1.getx(), evildoer1.gety(), null);

		// for(int i = 0; )
		if(PixelList.size()==0)
		{
			//add 20 pixels when pixellist is empty
			generatePixels(20);
			level++;

			Toast.makeText(context , "Level "  +  Integer.toString(level), Toast.LENGTH_SHORT).show();
			//run the folliowing if difficutly set to 1

			if(Difficulty ==1)
			{
				Log.d("Size",Integer.toString(EvilDoerList.size()));
				//do this as many times as EvilDoerList.size is
				for(int i = 0; i<EvilDoerList.size(); i++ )
				{
					Log.d("Difficulty","1");
					// Add 2 to the speed of evil doer and add 1 evildoer    
					if(level < 11)
					{
						EvilDoerList.get(i).setxvel(EvilDoerList.get(i).getxvel()+1);
						EvilDoerList.get(i).setyvel(EvilDoerList.get(i).getyvel()+1);
					}

				}
				if(level%3==0 && level<16)
				{
					generateEvilDoer(1);
				}
			}
			//if difficulty is 2
			if(Difficulty ==2){

				Log.d("Difficulty","2");
				// do this as many times as there are evildoers
				for(int i = 0; i<EvilDoerList.size(); i++ )
				{
					//increment speed by 4 and add 1 evildoer
					if(level<10){	
						EvilDoerList.get(i).setxvel(EvilDoerList.get(i).getxvel()+2);
						EvilDoerList.get(i).setyvel(EvilDoerList.get(i).getyvel()+2);
					}
					else
					{
						EvilDoerList.get(i).setxvel(EvilDoerList.get(i).getxvel()+1);
						EvilDoerList.get(i).setyvel(EvilDoerList.get(i).getyvel()+1);
					}
				}
				if(level<15 && level%2==0 )
				{
					generateEvilDoer(1);
				}
			}
			if(Difficulty ==3){
				Log.d("Difficulty", "3");
				for(int i = 0; i<EvilDoerList.size(); i++)
				{
					if(level<11)
				{
					EvilDoerList.get(i).setxvel(EvilDoerList.get(i).getxvel()+3);
					EvilDoerList.get(i).setyvel(EvilDoerList.get(i).getyvel()+3);
				}
				else
				{
					EvilDoerList.get(i).setxvel(EvilDoerList.get(i).getxvel()+2);
					EvilDoerList.get(i).setyvel(EvilDoerList.get(i).getyvel()+2);
				}
				}

				for(int i=0; i<PixelList.size(); i++)
				{
					if(PixelList.get(i).getsidelength()>5)
					{
						PixelList.get(i).setsidelength(PixelList.get(i).getsidelength());
					}
				}
				if(level<11)
				{
					generateEvilDoer(1);
				}
			}

		}

		//do nothing if else
		else
		{

		}




		invalidate();


	}

	public Ball getBall() {
		return ball1;
	}

	public int getLives()
	{
		return ball1.getLives();
	}


	public void reset() {
		// TODO Auto-generated method stub

	}





}